package com.taobao.shopcart.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taobao.shopcart.model.po.OrderProcess;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author erebus
 */
@Mapper
public interface OrderProcessMapper extends BaseMapper<OrderProcess> {

    @Select("select * from order_process  where  id % #{shardTotal} = #{shardIndex} and (status=1 or status=3) and fail_count<3 limit #{count}")
    List<OrderProcess> selectListByShardIndex(@Param("shardTotal") int shardTotal, @Param("shardIndex") int shardIndex, @Param("count") int count);


    /**
     * 开启一个任务 status=4代表此任务执行中
     * @param taskId 任务 process_id
     * @return 更新记录数
     */
    @Update("update order_process set status=4 where (status=1 or status=3) and fail_count<3 and id=#{taskId}")
    int startTask(@Param("taskId") long taskId);

    /**
     * 根据订单id获取
     */
    @Select("select * from order_process where order_id=#{orderId}")
    OrderProcess selectByOrderId(@Param("orderId") long orderId);

    /**
     * 根据id更新type
     */
    @Update("update order_process set status = 1, types=#{types} where id=#{id}")
    int updateTypeById(@Param("id") long id,@Param("types") int types);
}
